﻿using gt.webapi.core.Logging;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace gt.webapi.core.Handlers
{
    public class LogHandler : DelegatingHandler
    {
        private ApiLogger m_logger = null;
        public LogHandler(ApiLogger logger)
        {
            this.m_logger = logger;
        }

        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            await request.Content.LoadIntoBufferAsync();

            return await base.SendAsync(request, cancellationToken).ContinueWith((task) =>
            {
                var response = task.Result;
                this.m_logger.Info(request, response);
                return response;
            }, cancellationToken);
        }
    }
}
